﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Skywave_Code_Lines_Counter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private long counter;
        private List<string> extsToSearch = new List<string>();

        private void buttonStart_Click(object sender, EventArgs e)
        {
            if (foldersToSearch_List.Items.Count > 0)
            {
                extsToSearch.Clear();
                foreach (string fe1 in textBoxSuffix.Text.Split(new char[] { ';' }))
                {
                    extsToSearch.Add(fe1);
                }
                if (extsToSearch.Count > 0)
                {
                    this.counter = 0L;
                    this.Enabled = false;
                    backgroundWorker1.RunWorkerAsync();
                }
            }
        }

        private void CountLines(string p)
        {
            DirectoryInfo di = new DirectoryInfo(p);
            foreach (string feExt in extsToSearch)
            {
                foreach (FileInfo fileInfo in di.GetFiles(feExt))
                {
                    if (!fileInfo.Name.ToLower().Contains(".designer"))
                    {
                        StreamReader sr = new StreamReader(fileInfo.OpenRead());
                        while (!sr.EndOfStream)
                        {
                            string str1 = sr.ReadLine().Trim().ToLower();
                            if (str1.Contains("windows form designer generated code"))
                            {
                                while (!sr.EndOfStream && !str1.Contains("end region"))
                                {
                                    str1 = sr.ReadLine();
                                }
                            }
                            else if (((str1 != "") && (str1 != "}")) && (str1 != "{"))
                            {
                                this.counter += 1L;
                            }
                        }
                        sr.Close();
                    }
                }                
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                this.CountLines(dir.FullName);
            }
        }

        private void foldersToSearch_Browse_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
            {
                foldersToSearch_Add_Text.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        private void foldersToSearch_Add_Click(object sender, EventArgs e)
        {
            string str1 = foldersToSearch_Add_Text.Text;
            if (!foldersToSearch_List.Items.Contains(str1) && System.IO.Directory.Exists(str1))
            {
                foldersToSearch_List.Items.Add(str1);
                foldersToSearch_Add_Text.Text = "";
            }
        }

        private void foldersToSearch_List_SelectedIndexChanged(object sender, EventArgs e)
        {
            foldersToSearch_Remove.Enabled = foldersToSearch_List.SelectedItems.Count > 0;
        }

        private void foldersToSearch_Remove_Click(object sender, EventArgs e)
        {
            List<string> selectedItems = foldersToSearch_List.SelectedItems.Cast<string>().ToList();
            foreach (string fe1 in selectedItems)
            {
                foldersToSearch_List.Items.Remove(fe1);
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            foreach (string fe1 in foldersToSearch_List.Items)
            {
                CountLines(fe1);
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.textBoxResault.Text = this.counter.ToString();
            this.Enabled = true;
        }

        private void buttonAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this, "A software by 'SkyWave Corporation'.\nDesigner and Developer: Mostafa Aghajani", "About ...", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Icon = Properties.Resources.calculator;
        }

    }
}
